package com.itxiuyixiu.volunteer.controller;

import com.itxiuyixiu.tools.util.returns.ReturnVo;
import com.itxiuyixiu.volunteer.common.errors.ServiceException;
import com.itxiuyixiu.volunteer.dto.AdminForgetPasswordParam;
import com.itxiuyixiu.volunteer.dto.AdminModifyMessageParam;
import com.itxiuyixiu.volunteer.dto.AdminModifyPasswordParam;
import com.itxiuyixiu.volunteer.dto.AdminRegisterParam;
import com.itxiuyixiu.volunteer.service.AdminCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;

/**
 * @author 黄磊
 * @date 2020/5/1
 */
@Api(tags = "管理员通用API")
@RequestMapping("/admin")
@Validated
@RestController
public class AdminCommonController {
    @Autowired
    private AdminCommonService adminCommonService;

    @ApiOperation(value = "管理员注册", response = String.class, notes = "无权限限制")
    @PostMapping("/register")
    public ReturnVo saveAdmin(@Valid AdminRegisterParam admin, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ReturnVo.getFailureReturn(bindingResult);
        }
        try {
            adminCommonService.saveAdmin(admin);
            return ReturnVo.getSuccessReturn("恭喜您成功注册，请等待审核");
        } catch (ServiceException e) {
            return ReturnVo.getFailureReturn(e.getMessage());
        }
    }

    @ApiOperation(value = "获取当前管理员信息", response = Object.class, notes = "权限：需要管理员登录")
    @GetMapping("")
    public ReturnVo findAdmin() {
        try {
            return ReturnVo.getSuccessReturn(adminCommonService.findAdmin());
        } catch (ServiceException e) {
            return ReturnVo.getFailureReturn(e.getMessage());
        }
    }

    @ApiOperation(value = "修改当前管理员信息", response = String.class, notes = "权限：需要管理员登录")
    @PutMapping("")
    public ReturnVo modifyAdmin(@Valid AdminModifyMessageParam admin, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ReturnVo.getFailureReturn(bindingResult);
        }
        try {
            adminCommonService.modifyAdmin(admin);
            return ReturnVo.getSuccessReturn("成功修改信息");
        } catch (ServiceException e) {
            return ReturnVo.getFailureReturn(e.getMessage());
        }
    }

    @ApiOperation(value = "修改当前管理员密码", response = String.class, notes = "权限：需要管理员登录")
    @PutMapping("/account/password")
    public ReturnVo modifyPassword(@Valid AdminModifyPasswordParam dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ReturnVo.getFailureReturn(bindingResult);
        }
        try {
            adminCommonService.modifyPassword(dto.getOldPassword(), dto.getNewPassword());
            return ReturnVo.getSuccessReturn("成功修改密码");
        } catch (ServiceException e) {
            return ReturnVo.getFailureReturn(e.getMessage());
        }
    }

    @ApiOperation(value = "忘记密码", response = String.class)
    @PutMapping("/password")
    public ReturnVo forgetPassword(AdminForgetPasswordParam dto) {
        try {
            adminCommonService.forgetPassword(dto);
            return ReturnVo.getSuccessReturn("成功修改密码");
        } catch (ServiceException e) {
            return ReturnVo.getFailureReturn(e.getMessage());
        }
    }

    @ApiOperation(value = "查看邮箱是否被使用", response = String.class, notes = "返回的状态为200就代表邮箱未被注册过")
    @GetMapping("/register/email")
    public ReturnVo isEmailUsedOrNot(@NotEmpty(message = "邮箱不能为空") @Email(message = "邮箱格式不正确") String email) {
        if (adminCommonService.isEmailUsed(email)) {
            return ReturnVo.getFailureReturn("邮箱已经被他人使用，请换一个邮箱");
        } else {
            return ReturnVo.getSuccessReturn("邮箱未被注册过");
        }
    }
}
